<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no"/>
    <script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=kviF2GeQXkhb0NXyhdRbODFAe5GKb1x3"></script>
    <title>关键字输入提示词条</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style type="text/css">
        body, html, #app2 {
            width: 100%;
            height: 100%;
            margin: 0;
            font-family: "微软雅黑";
            font-size: 14px;
        }

        #l-map {
            height: 100%;
            width: 100%;
        }

        .map2 {
            width: 100%;
            height: 430px;
        }

        #r-result {
            width: 100%;
        }
    </style>
</head>
<body>

<div id="app2">

    <el-row>
        <el-col :span="16">
            <div id="r-result">
                <el-input v-model="input" id="suggestId" placeholder="请输入地址"></el-input>
            </div>
            <div id="searchResultPanel"
                 style="border:1px solid #C0C0C0;width:150px;height:auto; display:none;">
            </div>
        </el-col>
        <el-col :span="0.5">&nbsp;</el-col>
        <el-col :span="7">
            <el-button type="primary" @click="add">确定添加</el-button>
        </el-col>
    </el-row>
    <el-row>

        <div style="width: 100%;height: 400px">
            <div id="l-map" style="margin-top: 10px"></div>
        </div>
    </el-row>

</div>


</body>
</html>
<script type="text/javascript">
    var vue = new Vue({
        el: '#app2',
        data: {
            input: '',
            myValue: '',
            // address: '',
            longitude: null,
            latitude: null
        },
        created() {
            this.input="";
        },
        mounted() {
            var _this = this;
            // 百度地图API功能
            function G(id) {
                return document.getElementById(id);
            }

            var map = new BMap.Map("l-map");
            map.centerAndZoom("武汉", 12);
            map.enableScrollWheelZoom(true);// 初始化地图,设置城市和地图级别。

            var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
                {
                    "input": "suggestId"
                    , "location": map
                });

            ac.addEventListener("onhighlight", function (e) {  //鼠标放在下拉列表上的事件
                var str = "";
                var _value = e.fromitem.value;
                var value = "";
                if (e.fromitem.index > -1) {
                    value = _value.province + _value.city + _value.district + _value.street + _value.business;
                }
                str = "FromItem<br />index = " + e.fromitem.index + "<br />value = " + value;

                value = "";
                if (e.toitem.index > -1) {
                    _value = e.toitem.value;
                    value = _value.province + _value.city + _value.district + _value.street + _value.business;
                }
                str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
                G("searchResultPanel").innerHTML = str;

            });

            var myValue;
            ac.addEventListener("onconfirm", function (e) {    //鼠标点击下拉列表后的事件
                var _value = e.item.value;
                myValue = _value.province + _value.city + _value.district + _value.street + _value.business;
                _this.input = myValue;
                G("searchResultPanel").innerHTML = "onconfirm<br />index = " + e.item.index + "<br />myValue = " + myValue;
                setPlace();
            });

            function setPlace() {
                map.clearOverlays();    //清除地图上所有覆盖物
                function myFun() {
                    var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
                    map.centerAndZoom(pp, 18);
                    map.addOverlay(new BMap.Marker(pp));    //添加标注
                }

                var local = new BMap.LocalSearch(map, { //智能搜索
                    onSearchComplete: myFun
                });
                local.search(myValue);
            }

            function showInfo(e) {
                map.clearOverlays();
                _this.longitude = e.point.lng;
                _this.latitude = e.point.lat;
                var mk = new BMap.Marker(e.point);
                map.addOverlay(mk);
                map.panTo(e.point);
            }

            map.addEventListener("click", showInfo);
        },
        methods: {
            geocodeSearch(add) {
                var myGeo = new BMap.Geocoder();
                var _self=this;
                myGeo.getPoint(add, function (point) {
                    if (point) {
                        _self.input = add;
                        _self.longitude = point.lng;
                        _self.latitude = point.lat;
                        console.log("1:"+_self.longitude+"|"+_self.latitude)
                    }
                }, "武汉市");
            },
            add() {
                this.geocodeSearch(this.input);
                var param;
                console.log("2:"+this.longitude+"|"+this.latitude);
                if (this.input!= null && this.input!= '') {
                    param = {
                        address: this.input,
                        longitude: this.longitude,
                        latitude: this.latitude
                    };
                    console.log("3:"+this.longitude+"|"+this.latitude);
                    axios.post("/mapinfo/add.do", param).then((res) => {
                        if (res.data.flag) {
                            parent.vue.$message.success(res.data.message);
                            //主副页面通信
                            parent.vue.findPage();
                            parent.vue.findAllMap();
                        } else {
                            this.$message.error(res.data.message)
                        }
                    });
                }else {
                    this.$message.error("地址不能为空");
                }

            }
        }
    })

</script>